package com.educatioin.manage.wechat.controller;

import com.educatioin.manage.common.beans.Response;
import com.educatioin.manage.wechat.request.AddShoppingCartReq;
import com.educatioin.manage.wechat.request.CustomerLoginReq;
import com.educatioin.manage.wechat.request.CustomerSignReq;
import com.educatioin.manage.wechat.response.*;
import com.educatioin.manage.wechat.service.WeChatCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by BensonZc on 2018/4/22.
 */
@RestController
@RequestMapping("/wx/api/customer")
public class WeChatCustomerController {

    @Autowired
    private WeChatCustomerService weChatCustomerService;


    /**
     * 客户登录接口
     * @param customerLoginReq
     * @return
     * @throws IOException
     */
    @RequestMapping("/sign")
    @ResponseBody
    public CustomerSignRsp customerSign(HttpServletRequest request, CustomerSignReq customerLoginReq) {
        return weChatCustomerService.customerSign(request, customerLoginReq);
    }

    /**
     * 成功购买直接登录
     * @param phone
     * @return
     * @throws IOException
     */
    @RequestMapping("/buyLogin")
    @ResponseBody
    public CustomerLoginRsp buyLogin(HttpServletRequest request, String phone) {
        return weChatCustomerService.buyLogin(request, phone);
    }

    /**
     * 客户登录接口
     * @param customerLoginReq
     * @return
     * @throws IOException
     */
    @RequestMapping("/login")
    @ResponseBody
    public CustomerLoginRsp customerLogin(HttpServletRequest request, CustomerLoginReq customerLoginReq) {
        return weChatCustomerService.customerLogin(request, customerLoginReq);
    }

    @RequestMapping("/getCustomerInfo")
    @ResponseBody
    public GetCustomerInfoRsp getCustomerInfo(HttpServletRequest request){
        Object token = request.getSession().getAttribute("token");
        if(null == token){
            GetCustomerInfoRsp getCustomerInfoRsp = new GetCustomerInfoRsp();
            getCustomerInfoRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            getCustomerInfoRsp.setReturnMsg("客户未登录！");
            return getCustomerInfoRsp;
        }
        return weChatCustomerService.getCustomerInfo(token.toString());
    }

    @RequestMapping("/collect")
    @ResponseBody
    public Response collectProduct(HttpServletRequest request, Long productId, Integer productType){
        return weChatCustomerService.collectProduct(request, productId, productType);
    }

    @RequestMapping("/shoppingCart")
    @ResponseBody
    public Response shoppingCart(@RequestBody AddShoppingCartReq addShoppingCartReq, HttpServletRequest request){
        try {
            return weChatCustomerService.shoppingCart(request, addShoppingCartReq);
        } catch (IOException e) {
            e.printStackTrace();
            Response response = new Response();
            response.setReturnCode(Response.RETURN_FAIL_CODE);
            return response;
        }
    }

    @RequestMapping("/myCollection")
    @ResponseBody
    public MyCollectionRsp myCollection(HttpServletRequest request){
        return weChatCustomerService.myCollection(request);
    }

    @RequestMapping("/myShoppingCarts")
    @ResponseBody
    public MyShoppingCartRsp myShoppingCarts(HttpServletRequest request){
        return weChatCustomerService.myShoppingCarts(request);
    }
}
